<template>
  <div>
    <a-button @click="handleStateFilter('state_draft')"> 待处理 </a-button>
    <!-- <a-button @click="handleStateFilter('state_error')"> error </a-button> -->
    <a-button @click="handleStateFilter('')"> 全部 </a-button>
    <a-button @click="handleStateFilter('state_done')"> 已完成 </a-button>

    <!-- 根据 用户 过滤 -->

    模糊搜索客户名称 partner_id
    <a-input v-model="filter_partner_id_value"> </a-input>

    模糊搜索NC发票号 invoice_num
    <a-input v-model="filter_invoice_num_value"> </a-input>

    <a-button @click="handleFilter"> 搜索 </a-button>

    <a-divider type="vertical" />
    <a-button @click="deleteOds"> 删除 </a-button>

    <a-table
      :row-selection="rowSelection"
      :columns="columns"
      :data-source="records"
      rowKey="id"
    >
    </a-table>

    <ods2so ref="refWizard" @ods2so-done="handleOds2so" />
  </div>
</template>

<script>
import api from '@/odoorpc'

import ods2so from './ods2so.vue'

import simpleTreeMixin from '../simpleTreeMixin'

export default {
  name: 'OdsTreeView',
  components: { ods2so },
  mixins: [simpleTreeMixin],
  props: {},

  data() {
    return {
      filter_partner_id_value: '',
      filter_invoice_num_value: ''
    }
  },

  computed: {
    columns() {
      return [
        {
          dataIndex: 'invoice_num',
          key: 'invoice_num',
          title: '业务单据号',
          ellipsis: true
        },
        {
          dataIndex: 'company_id',
          key: 'company_id',
          title: '公司',
          customRender: value => (value ? value[1] : ''),
          ellipsis: true
        },

        {
          dataIndex: 'partner_id',
          key: 'partner_id',
          title: '客户',
          customRender: value => (value ? value[1] : ''),
          ellipsis: true
        },
        {
          dataIndex: 'user_id',
          key: 'user_id',
          title: '用户',
          customRender: value => (value ? value[1] : ''),
          ellipsis: true
        },
        {
          dataIndex: 'amount_total',
          key: 'amount_total',
          title: '总金额',
          ellipsis: true
        },
        {
          dataIndex: 'create_date',
          key: 'create_date',
          title: '创建时间',
          ellipsis: true
        },
        {
          dataIndex: 'state',
          key: 'state',
          title: '状态',
          customRender: value => {
            const meta = this.fields['state']
            const elm = meta.selection.find(item => item[0] === value)
            return elm ? elm[1] : ''
          },
          ellipsis: true
        }
        // {
        //   dataIndex: 'name',
        //   key: 'name',
        //   title: '订单编号',
        //   ellipsis: true
        // },
        // {
        //   dataIndex: 'client_order_ref',
        //   key: 'client_order_ref',
        //   title: 'NC发票PK',
        //   ellipsis: true
        // }
      ]
    }
  },

  created() {},

  mounted() {},

  methods: {
    async rowSelected() {
      this.showWizard()
    },

    async showWizard() {
      console.log(this.activeIds)
      console.log(this)
      // initMe

      const default_invoice_type = this.treeview.context.default_invoice_type

      await this.$refs.refWizard.initMe(this.activeIds, {
        context: { default_invoice_type }
      })
      this.$refs.refWizard.handleMergeType('push_plus')
      // call wizard
    },

    init_filter() {
      this.search_values = this.treeview.search_change(
        this.search_items.group1,
        ['state_draft']
      )
    },

    async handleFilter() {
      const val_p = this.filter_partner_id_value
      this.search_values = this.treeview.search_change(
        this.search_items.partner_id,
        [{ string: val_p, value: val_p }]
      )

      const val_i = this.filter_invoice_num_value
      this.search_values = this.treeview.search_change(
        this.search_items.invoice_num,
        [val_i]
      )

      this.records = await this.treeview.search_read()
    },

    async handleStateFilter(state_value) {
      this.search_values = this.treeview.search_change(
        this.search_items.group1,
        [state_value]
      )

      this.records = await this.treeview.search_read()
    },

    handleButtonClick(e) {
      console.log('click left button', e)
    },
    handleMenuClick(e) {
      console.log('click', e)
    },
    async deleteOds() {
      console.log(this.activeIds)
      // await this.treeview.unlink(this.activeIds)//这两个函数等效
      await api.env.model('fp.ods.sale.order').unlink(this.activeIds)
      this.records = await this.treeview.search_read()
    },
    async handleOds2so() {
      this.records = await this.treeview.search_read()
    }
  }
}
</script>

<style type="text/css"></style>
